package org.dromara.comm.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.dromara.comm_lhz.util.QiniuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 上传图片控制层
 *
 * @author: 郭伟明
 * @date: 2024-07-31 20:17
 **/
@RestController
@RequestMapping("/image")
@Tag(name = "图片上传")
public class ImageController {

    @Autowired
    private QiniuUtil qiniuUtil;

    @Operation(summary = "批量上传图片")
    @PostMapping("/uploadImages")
    public ResponseEntity<Object> uploadImages(@RequestParam("file") List<MultipartFile> files) throws Exception {
        // 验证文件列表不为空
        if (files == null || files.isEmpty()) {
            return new ResponseEntity<>("No files were uploaded", HttpStatus.BAD_REQUEST);
        }

        // 调用上传服务处理每个文件
        List<String> uploadedUrls = new ArrayList<>();
        for (MultipartFile file : files) {
            // 调用你的上传工具类
            String uploadedUrl = qiniuUtil.uploadImage(file);
            uploadedUrls.add(uploadedUrl);
        }

        // 返回上传的文件URL列表
        return new ResponseEntity<>(uploadedUrls, HttpStatus.OK);
    }

}
